package com.d3.d3xmpp.activites;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.json.JSONException;
import org.json.JSONObject;

import com.d3.d3xmpp.R;
import com.d3.d3xmpp.constant.Constants;
import com.d3.d3xmpp.d3View.D3View;
import com.d3.d3xmpp.util.AsyncHttpUtil;
import com.d3.d3xmpp.util.MyAndroidUtil;
import com.d3.d3xmpp.util.Tool;
import com.d3.d3xmpp.util.Util;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.ResponseHandlerInterface;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author MZH
 *
 */
public class RegActivity extends BaseActivity {
	private static Logger log = Logger.getAnonymousLogger();
	@D3View TextView nameText,pwdText,pwdText1,emailText;//,emailText,phoneText
	@D3View(click="onClick") Button registBtn;
	String name,pwd;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.acti_register);
		initTitle();
	}
	
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.registBtn:
			name = nameText.getText().toString();
			pwd = pwdText.getText().toString();
			String againPwd = pwdText1.getText().toString();
			if (TextUtils.isEmpty(name)) {
				Tool.initToast(RegActivity.this, getString(R.string.register_name));
			}
			else if (TextUtils.isEmpty(pwd)) {
				Tool.initToast(RegActivity.this, getString(R.string.register_password));
			} else if (TextUtils.isEmpty(againPwd)) {
				Tool.initToast(RegActivity.this, getString(R.string.register_again_password));
			} else if (!againPwd.equals(pwd)) {
				Tool.initToast(RegActivity.this, getString(R.string.register_password_defferent));
			} else if (emailText.equals("") || !Util.getInstance().isEmail(emailText.getText().toString())) {
				Tool.initToast(RegActivity.this, getString(R.string.register_email_error));
			}
			else {
				createAccount(name, pwd);
			}
			break;

		default:
			break;
		}
	}
	
	
	private void createAccount(final String userName, final String passWord) {
		final Map<String, Object> params = new HashMap<String, Object>();
		params.put("username", userName);
		params.put("password", passWord);
		params.put("email", emailText.getText().toString());
		
		AsyncHttpUtil.post("/account/register", params, getApplicationContext(), new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
				Log.d(Constants.SYSTEM_OUT, params.toString());
				Log.d(Constants.SYSTEM_OUT, response.toString());
				try {
					if (response == null) {
						Tool.initToast(getApplicationContext(), getString(R.string.service_result));
					}
					if (response.getInt("errcode") == 0) {
						Tool.initToast(getApplicationContext(), getString(R.string.register_success));
						Constants.USER_NAME = name;
						Constants.PWD = pwd;
						MyAndroidUtil.editXmlByString(Constants.LOGIN_ACCOUNT, name);
						MyAndroidUtil.editXmlByString(Constants.LOGIN_PWD, pwd);
						MyAndroidUtil.editXml(Constants.LOGIN_CHECK, true);

						Intent intent = new Intent(RegActivity.this, MainActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intent);
						finish();
					} else {
						if (response.getInt("errcode") != 0) {
							Tool.initToast(getApplicationContext(), response.getString("errmsg"));
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onPostProcessResponse(ResponseHandlerInterface instance, HttpResponse response) {
				super.onPostProcessResponse(instance, response);
			}
		});
	}
	
}
